﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentManagerSystem
{
    public class InputManager
    {
        StudentsSystem StudentsSystem;
        public InputManager(StudentsSystem ss)
        {
            StudentsSystem = ss;
            Console.Title = "学生成绩管理系统";
            Console.SetWindowSize(88, 50);
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
        }
        //登录界面
        public void LoginPanel()
        {
            //清屏
            Console.Clear();
            PrintTitle();
            PrintMenu(1, "输入账号密码");
            PrintMenu(2, "退出系统");
            PrintMenuEnd();

            
            bool flag= false;
            do
            {
                int op;
                if (flag)
                    Write("输入错误，请重新输入需要操作的编号:");
                else
                    Write("请输入需要操作的编号:");
                flag = false;
                while (!int.TryParse(Console.ReadLine(), out op))
                {
                    Write("\n输入错误，请重新输入:");
                }
                Console.WriteLine(op);
                switch (op)
                {
                    case 1:
                        InputAccountPassword();
                        return;
                    case 2:
                        return;
                    default:
                        flag = true;
                        break;
                }
            } while (flag);
        }

        //主界面
        public void MainPanel()
        {
            //清屏
            Console.Clear();
            PrintTitle();
            PrintMenu(1, "信息录入");
            PrintMenu(2, "信息查询");
            PrintMenu(3, "信息修改");
            PrintMenu(4, "成绩分析");
            PrintMenu(5, "成绩排名");
            PrintMenu(6, "返回上一层");
            PrintMenuEnd();

            bool flag = false;
            do
            {
                int op;
                if (flag)
                    Write("输入错误，请重新输入需要操作的编号:");
                else
                    Write("请输入需要操作的编号:");
                flag = false;
                while (!int.TryParse(Console.ReadLine(), out op))
                {
                    Write("\n输入错误，请重新输入需要操作的编号:");
                }
                Console.WriteLine(op);
                switch (op)
                {
                    case 1:
                        NNPanel();
                        return;
                    case 2:
                        MainPanel();
                        return;
                    case 3:
                        MainPanel();
                        return;
                    case 4:
                        MainPanel();
                        return;
                    case 5:
                        MainPanel();
                        return;
                    case 6:
                        LoginPanel();
                        return;
                    default:
                        flag = true;
                        break;
                }
            } while (flag);

        }
        public void NNPanel()
        {
            PrintLineStart();
            Write("欢迎来到牛哥时间~");
            Console.ReadLine();
        }
        //输入账号密码
        private void InputAccountPassword()
        {
            bool flag = false;
            do
            {

                if (flag)
                    Write("账号或密码错误，请重新输入账号:");
                else
                    Write("请输入账号:");
                flag = false;
                string account = Console.ReadLine();

                Write("\n请输入密码:");
                string password = Console.ReadLine();

                if (StudentsSystem.CheckAccountPassword(account, password))
                {
                    MainPanel();
                    return;
                }
                else
                {
                    flag = true;
                }

            } while (flag);
        }

        //输出一行菜单
        private void PrintMenu(int index, string s)
        {
            PrintLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("            ***  {0}  ***    |    {1}", index, s);
            Console.ForegroundColor = ConsoleColor.Black;
            PrintLine();
        }

        private void Write(string s, ConsoleColor c = ConsoleColor.Black)
        {
            Console.ForegroundColor = c;
            Console.Write(s);
            Console.ForegroundColor = ConsoleColor.Black;
        }

        //输出一行*
        private void PrintLineStart()
        {
            Console.WriteLine("**************************************************************************************");
        }

        //输出一个空行
        private void PrintLine()
        {
            Console.WriteLine();
        }

        //输出学生系统菜单尾
        private void PrintMenuEnd()
        {
            PrintLineStart();
            PrintLineStart();
        }

        //输出学生系统标题头
        private void PrintTitle()
        {
            PrintLineStart();
            Console.WriteLine("*********************************  学生成绩管理系统  *********************************");
            PrintLineStart();
        }
    }
}
